<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2016/7/28
 * Time: 7:02
 */

namespace Colin\Util;


class Helper
{
    /**
     *  用于实现异步脚本的函数
     *  eg:
     *  set_time_limit(0);
     *  Helper::bootAsync('程序已经后台执行');
     *  程序正常执行代码 .........
     *
     * @param $output
     * @param string $content_type
     * @param int $length
     * @param bool $wait
     */
    public static function bootAsync($content, $content_type = 'application/json; charset=utf-8', $length = 0, $wait = true)
    {

        // Calculate Content Lenght
        if ($length == 0) {
            $content .= "\n";
            $length = (mb_strlen($content, '8bit') - 1);
        }
        // Prime php for background operations
        $loop = 0;
        while (ob_get_level() && $loop < 25) {
            ob_end_clean();
            $loop++;
        }
        header("Connection: close");
        ignore_user_abort();

        // Output headers &amp; data
        ob_start();
        header("Content-type: " . $content_type);
        header("Expires: Sun, 19 Nov 1978 05:00:00 GMT");
        header("Cache-Control: no-cache");
        header("Cache-Control: must-revalidate");
        header("Content-Length: " . $length);
        header("Connection: close");
        print($content);
        ob_end_flush();
        flush();
        // wait for 1 second
        if ($wait) {
            sleep(1);
        }

        // text returned and connection closed.
        // Do background processing. Time taken after should not effect page load times.
    }

    public static function UTF82GBK($data)
    {
        if(is_numeric($data))
            return $data;
        if (is_string($data))
            return iconv("UTF-8","gbk//IGNORE",  $data);
        if (is_array($data)){
            return array_combine(array_map([static::class,'UTF82GBK'],array_keys($data)), array_map([static::class,'toGBK'],array_values($data)));
        }
        if (is_object($data))
            return call_user_func([static::class,'UTF82GBK'], (array)$data);
    }

    public static function GBK2UTF8($data)
    {
        if(is_numeric($data))
            return $data;
        if (is_string($data))
            return iconv("gbk", "UTF-8//IGNORE", $data);
        if (is_array($data)){
            return array_combine(array_map([static::class,'GBK2UTF8'],array_keys($data)), array_map([static::class,'toUTF8'],array_values($data)));
        }
        if (is_object($data))
            return call_user_func([static::class,'GBK2UTF8'], (array)$data);
    }

    public static function toUTF8($data)
    {
        if(is_numeric($data))
            return $data;
        if (is_string($data))
            return mb_check_encoding($data, "UTF-8") ? $data : mb_convert_encoding($data, "UTF-8", ["GB18030"]);
        if (is_array($data)){
            return array_combine(array_map([static::class,'toUTF8'],array_keys($data)), array_map([static::class,'toUTF8'],array_values($data)));
        }
        if (is_object($data))
            return call_user_func([static::class,'toUTF8'], (array)$data);
    }

    public static function toGBK($data)
    {
         if(is_numeric($data))
            return $data;
        if (is_string($data))
            return mb_check_encoding($data, "GB18030") ? $data : mb_convert_encoding($data, "GB18030", ["UTF-8"]);
        if (is_array($data)){
            return array_combine(array_map([static::class,'toGBK'],array_keys($data)), array_map([static::class,'toUTF8'],array_values($data)));
        }
        if (is_object($data))
            return call_user_func([static::class,'toGBK'], (array)$data);
    }

    public static function uuid()
    {
        if (function_exists('com_create_guid')){
            return com_create_guid();
        }else{
            mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
            $charid = strtoupper(md5(uniqid(rand(), true)));
            $hyphen = chr(45);// "-"
            $uuid = chr(123)// "{"
                .substr($charid, 0, 8).$hyphen
                .substr($charid, 8, 4).$hyphen
                .substr($charid,12, 4).$hyphen
                .substr($charid,16, 4).$hyphen
                .substr($charid,20,12)
                .chr(125);// "}"
            return $uuid;
        }
    }

    public static function isWinNT()
    {
        return php_uname("s") === "Windows NT";
    }
}